/**
 * @fileOverview
 * @author rekey
 * Created by rekey on 23/5/14.
 */

module.exports = function * (next) {
  var response = this.response;
  var request = this.request;
  response.json = function (obj) {
    response.type = '.json';
    try {
      response.body = JSON.stringify(obj);
    } catch (e) {
      response.body = '{}';
    }
  };
  response.jsonp = function (obj) {
    if (!request.query.callback) {
      response.json(obj);
    } else {
      response.type = '.js';
      var ret = '{}';
      try {
        ret = JSON.stringify(obj);
      } catch (e) {
      }
      response.body = 'window.' + request.query.callback + ' && ' + request.query.callback +
        '(' + ret + ');';
    }
  };
  yield next;
};